package com.neusoft.shry.dao;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.shry.dto.EmpDto;
import com.zero.basic.dao.DefaultBaseDao;
import com.zero.basic.dao.SqlListBaseDao;
import com.zero.basic.dao.SqlPagingListBaseDao;
import com.zero.basic.dao.SqlQueryBaseDao;
import com.zero.basic.exception.SmsDaoException;
import com.zero.basic.model.Pager;
import com.zero.mms.model.Employee;
import com.zero.mms.model.Position;

@Repository("empDao")
@Transactional(rollbackFor = Exception.class)
public class EmpDaoImp extends DefaultBaseDao<Employee> implements EmpDao {

	@Inject
	private SqlQueryBaseDao<?> sqlQueryBaseDao;
	
	@Inject
	private SqlPagingListBaseDao<?> sqlPagingListBaseDao;
	
	@Inject
	private SqlListBaseDao<?> sqlListBaseDao;
	
	//get an employee by id
	@Override
	public Employee getEmpById(Integer emp_id) throws SmsDaoException {
		try{
			String sql = "select * from t_emp where emp_id = ?";
			Employee employee = (Employee) sqlQueryBaseDao.queryBySql(sql, emp_id, Employee.class, true, true);
			return employee;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

	//get employees by conditions
	@Override
	public Pager<EmpDto> getEmpByCondition(Integer emp_id, String emp_name,
			String dep_name) throws SmsDaoException {
		try{
			String sql = "select e.emp_id as empId,e.emp_name as empName,e.emp_sex as empSex,d.dep_name as depName,p.position_name as positionName "
					+ "from t_emp e, t_dep d, t_position p "
					+ "where e.dep_id=d.dep_id and e.position_id=p.position_id";
			if (emp_id!=null) {
				sql += " and e.emp_id ='" + emp_id +"'";
			}
			if (!"".equals(emp_name)&&!"*".equals(emp_name)&&emp_name!=null) {
				sql += " and e.emp_name like '%" + emp_name +"%'";
			}
			if (!"".equals(dep_name)&&!"*".equals(dep_name)&&dep_name!=null) {
				sql += " and d.dep_name like '%" + dep_name +"%'";
			}
			System.out.println(sql);
			Pager<EmpDto> pager = sqlPagingListBaseDao.findBySql(sql, EmpDto.class, false);
			return pager;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

	//get positions of a department
	@Override
	public List<Position> getDepPos(Integer dep_id) throws SmsDaoException {
		try{
			String sql = "select * from t_position where dep_id = ?";
			List<Position> positions = sqlListBaseDao.listBySql(sql, dep_id, Position.class, true, true);
			return positions;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsDaoException();
		}
	}

}
